<?php

namespace app\common\model\marketing\label;

use addons\xyb\XypHelper;
use think\Model;
use function Couchbase\defaultDecoder;

class Level extends Model
{
    // 数据库
    protected $name = 'label_level';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'status_text',
        'create_date'

    ];

    public function getStatusTextAttr($value, $data)
    {
        if(!isset($data['status'])) return '';
        return $data['status'] == 'normal' ? '启用' : '禁用';
    }

    public function getCreateDateAttr($value, $data)
    {
        if (isset($data['createtime']) && $data['createtime']) {
            return date('Y-m-d H:i', $data['createtime']);
        } else {
            return '';
        }
    }

    public function getParentList($where = [], $page = 1, $limit = 10){

        $total = $this->where($where)->count();
        $list = $this->where($where)
            ->field('id,name,value,standard,description')
            ->page($page, $limit)
            ->order('id asc,weigh desc')
            ->select();
        foreach ($list as $key => $value){
            if(!$standard = XypHelper::formatJsonStringToArray($value['standard'])){}
            foreach (Category::$flag_lists as $k => $v){
                if(isset($standard[$k]) && $standard[$k]){
                    $list[$key][$k] = $standard[$k];
                }else{
                    $list[$key][$k] = 0;
                }
            }
        }
        return ['total' => $total, 'data' => $list];
    }
    public function getList($where = [], $page = 1, $limit = 10)
    {
        $total = $this->where($where)->count();
        $list = $this->where($where)
            ->page($page, $limit)
            ->order('id asc,weigh desc')
            ->select();

        return ['total' => $total, 'data' => $list];
    }

    public function add($data)
    {
        // 处理参数JSON
        if (isset($data['params']) && is_array($data['params'])) {
            $data['params'] = json_encode($data['params'], JSON_UNESCAPED_UNICODE);
        }

        return $this->allowField(true)->save($data);
    }


    public function edit($data)
    {
        // 处理参数JSON
        if (isset($data['params']) && is_array($data['params'])) {
            $data['params'] = json_encode($data['params'], JSON_UNESCAPED_UNICODE);
        }

        return $this->allowField(true)->save($data, ['id' => $data['id']]);
    }

    public function remove($id)
    {
        return $this->where('id', $id)->delete();
    }

    public function updateStatus($id, $status)
    {
        return $this->where('id', $id)->update(['status' => $status]);
    }


    public function updateParentLevelLists($level_lists, $category_id){
        $level_lists_forms = [];
        foreach ($level_lists as $key => $value){
            $standard_arr = [];
            $standard_keys = array_keys(Category::$flag_lists);
            foreach ($standard_keys as $k => $v){
                $standard_arr[$v]= isset($value[$v]) ? intval($value[$v]) : 0;
            }
            $level_lists_forms[$key] = [
                'name' => isset($value['name'])? $value['name'] : '',
                'description' => isset($value['description']) ? $value['description'] : '',
                'standard' => json_encode($standard_arr, JSON_UNESCAPED_UNICODE),
                'category_id' => $category_id,
                'status' => 'normal',
                'createtime' => time(),
            ];
        }
        $this->where('category_id', $category_id)->delete();
        return $this->insertAll(array_values($level_lists_forms));
    }

}
